package top.kaoshanji.ch06;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 代码清单 ch06-02：本地日期示例(6.2节)
 * author kaoshanji
 * ^_^
 * create time 2024/12/9 14:12
 */
public class LocalDates {

    private static final Logger logger = LogManager.getLogger(LocalDates.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch06-02：本地日期示例(6.2节)...");

        logger.info("...今天的本地日期...");
        LocalDate today = LocalDate.now();
        System.out.println("today: " + today);

        logger.info("...指定的本地日期...");
        LocalDate alonzosBirthday = LocalDate.of(1903, 6, 14);
        alonzosBirthday = LocalDate.of(1903, Month.JUNE, 14);
        System.out.println("alonzosBirthday: " + alonzosBirthday);

        logger.info("...指定的本地日期 添加 天数...");
        LocalDate programmersDay = LocalDate.of(2018, 1, 1).plusDays(255);
        System.out.println("programmersDay: " + programmersDay);

        logger.info("...两个本地日期的时长...");
        LocalDate independenceDay = LocalDate.of(2018, Month.JULY, 4);
        LocalDate christmas = LocalDate.of(2018, Month.DECEMBER, 25);

        System.out.println("Until christmas: " + independenceDay.until(christmas));
        System.out.println("Until christmas（天数）: " + independenceDay.until(christmas, ChronoUnit.DAYS));

        logger.info("...计算两个不存在的时间，得到的是最接近的时间...");
        // 2016年2月只有 29 天
        System.out.println(LocalDate.of(2016, 1, 31).plusMonths(1));
        System.out.println(LocalDate.of(2016, 3, 31).minusMonths(1));

        logger.info("...周...");
        DayOfWeek startOfLastMillennium = LocalDate.of(1900, 1, 1).getDayOfWeek();
        System.out.println("startOfLastMillennium: " + startOfLastMillennium);
        System.out.println(startOfLastMillennium.getValue());
        System.out.println(DayOfWeek.SATURDAY.plus(3));

        logger.info("...流...");
        LocalDate start = LocalDate.of(2000, 1, 1);
        LocalDate endExclusive = LocalDate.now();
        Stream<LocalDate> firstDaysInMonth = start.datesUntil(endExclusive, Period.ofMonths(1));
        System.out.println("firstDaysInMonth: " + firstDaysInMonth.collect(Collectors.toList()));

    }

}
